<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-card">
        <!-- Logo Section -->
        <div class="logo-section">
          <div class="logo-icon">
            <svg class="logo-symbol" width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
              <rect width="20" height="20" rx="2" fill="white"/>
              <path d="M10 2L2 18H18L10 2Z" fill="#2C2C2C"/>
              <circle cx="10" cy="10" r="3" fill="white"/>
            </svg>
          </div>
          <span class="logo-text">AI教育资源平台</span>
        </div>

        <!-- Header Section -->
        <div class="header-section">
          <h1 class="welcome-title">找回密码</h1>
          <p class="welcome-subtitle">
            <span v-if="step === 1">请输入注册时使用的邮箱地址，我们将向您发送验证码</span>
            <span v-else-if="step === 2">请输入收到的验证码</span>
            <span v-else>请设置新的密码</span>
          </p>
        </div>

        <!-- Step 1: Email Input -->
        <form v-if="step === 1" class="login-form" @submit.prevent="sendVerificationCode">
          <div class="form-group">
            <label class="form-label">邮箱</label>
            <div class="input-wrapper">
              <input
                  type="email"
                  v-model="email"
                  class="form-input"
                  placeholder="请输入邮箱地址"
                  required
              />
            </div>
          </div>

          <button type="submit" class="login-button" :disabled="isSending">
            {{ isSending ? '发送中...' : '发送验证码' }}
          </button>
        </form>

        <!-- Step 2: Verification Code -->
        <form v-else-if="step === 2" class="login-form" @submit.prevent="verifyCode">
          <div class="form-group">
            <label class="form-label">验证码</label>
            <div class="input-wrapper">
              <input
                  type="text"
                  v-model="verificationCode"
                  class="form-input"
                  placeholder="请输入6位验证码"
                  maxlength="6"
                  required
              />
            </div>
          </div>

          <div class="resend-section">
            <span class="resend-text">没有收到验证码？</span>
            <button 
              type="button" 
              class="resend-button" 
              :disabled="resendCountdown > 0"
              @click="resendCode"
            >
              {{ resendCountdown > 0 ? `${resendCountdown}s后重发` : '重新发送' }}
            </button>
          </div>

          <button type="submit" class="login-button" :disabled="isVerifying">
            {{ isVerifying ? '验证中...' : '验证' }}
          </button>
        </form>

        <!-- Step 3: New Password -->
        <form v-else class="login-form" @submit.prevent="resetPassword">
          <div class="form-group">
            <label class="form-label">新密码</label>
            <div class="input-wrapper">
              <input
                  :type="showPassword ? 'text' : 'password'"
                  v-model="newPassword"
                  class="form-input"
                  placeholder="请输入新密码"
                  required
              />
              <button 
                type="button" 
                class="password-toggle" 
                @click="showPassword = !showPassword"
              >
                <el-icon style="font-size: 18px;">
                  <component :is="showPassword ? View :Hide" />
                </el-icon>
              </button>
            </div>
          </div>

          <div class="form-group">
            <label class="form-label">确认密码</label>
            <div class="input-wrapper">
              <input
                  :type="showConfirmPassword ? 'text' : 'password'"
                  v-model="confirmPassword"
                  class="form-input"
                  placeholder="请再次输入新密码"
                  required
              />
              <button 
                type="button" 
                class="password-toggle" 
                @click="showConfirmPassword = !showConfirmPassword"
              >
                <el-icon style="font-size: 18px;">
                <component :is="showConfirmPassword ? View :Hide" />
              </el-icon>
              </button>
            </div>
          </div>

          <button type="submit" class="login-button" :disabled="isResetting">
            {{ isResetting ? '重置中...' : '重置密码' }}
          </button>
        </form>

        <!-- Back to Login -->
        <div class="signup-link">
          <span class="signup-text">想起密码了？</span>
          <router-link to="/login" class="signup-button">返回登录</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import {ElIcon, ElMessage} from 'element-plus'
import {sendVerificationCodeAPI,verifyCodeAPI} from "@/apis";
const router = useRouter()

// 步骤控制
const step = ref(1) // 1: 输入邮箱, 2: 输入验证码, 3: 设置新密码

// 表单数据
const email = ref('')
const verificationCode = ref('')
const newPassword = ref('')
const confirmPassword = ref('')

// 状态控制
const isSending = ref(false)
const isVerifying = ref(false)
const isResetting = ref(false)
const resendCountdown = ref(0)
const showPassword = ref(false)
const showConfirmPassword = ref(false)

// 定时器
let resendTimer: number | null = null

// 发送验证码
const sendVerificationCode = async () => {
  if (!email.value) return ElMessage.warning('请输入邮箱')
  
  isSending.value = true
  try {
    // 模拟 API 请求
    const res = await sendVerificationCodeAPI(
      email.value
  )
    if(res.code !==200)
    {
      throw new Error(res.message)
    }
    ElMessage.success('验证码已发送，请检查您的邮箱')
    step.value = 2
    startResendCountdown()
  } catch (err) {
    ElMessage.error('发送失败，请稍后重试'+err)
  } finally {
    isSending.value = false
  }
}

// 验证验证码
const verifyCode = async () => {
  if (!verificationCode.value) return ElMessage.warning('请输入验证码')
  if (verificationCode.value.length !== 6) return ElMessage.warning('请输入6位验证码')
  
  isVerifying.value = true
  try {
    // 模拟 API 请求
    const res = await verifyCodeAPI({email:email.value,verificationCode:verificationCode.value})
    if(res.code !==200)
    {
      throw new Error(res.message)
    }
    // 这里应该验证验证码是否正确
    ElMessage.success('验证码验证成功')
    step.value = 3
  } catch (err) {
    ElMessage.error('验证码错误，请重新输入')
  } finally {
    isVerifying.value = false
  }
}

// 重置密码
const resetPassword = async () => {
  if (!newPassword.value) return ElMessage.warning('请输入新密码')
  if (newPassword.value.length < 6) return ElMessage.warning('密码长度至少6位')
  if (!confirmPassword.value) return ElMessage.warning('请确认密码')
  if (newPassword.value !== confirmPassword.value) return ElMessage.warning('两次输入的密码不一致')
  
  isResetting.value = true
  try {
    // 模拟 API 请求
    await new Promise(resolve => setTimeout(resolve, 1000))
    ElMessage.success('密码重置成功，请使用新密码登录')
    router.push('/login')
  } catch (err) {
    ElMessage.error('密码重置失败，请稍后重试')
  } finally {
    isResetting.value = false
  }
}

// 重新发送验证码
const resendCode = async () => {
  if (resendCountdown.value > 0) return
  
  isSending.value = true
  try {
    // 模拟 API 请求
    await new Promise(resolve => setTimeout(resolve, 1000))
    ElMessage.success('验证码已重新发送')
    startResendCountdown()
  } catch (err) {
    ElMessage.error('发送失败，请稍后重试')
  } finally {
    isSending.value = false
  }
}

// 开始重发倒计时
const startResendCountdown = () => {
  resendCountdown.value = 60
  resendTimer = setInterval(() => {
    resendCountdown.value--
    if (resendCountdown.value <= 0 && resendTimer) {
      clearInterval(resendTimer)
      resendTimer = null
    }
  }, 1000)
}

// 组件卸载时清理定时器
import { onUnmounted } from 'vue'
import {Hide, View} from "@element-plus/icons-vue";
onUnmounted(() => {
  if (resendTimer) {
    clearInterval(resendTimer)
  }
})
</script>

<style scoped>
.login-page {
  width: 100vw;
  min-height: 100vh;
  background: linear-gradient(90deg, #F8FAFC 0%, #F1F5F9 50%, #E2E8F0 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Inter', sans-serif;
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 448px;
}

.login-card {
  background: white;
  border-radius: 8px;
  border: 1px solid #D9D9D9;
  padding: 48px 32px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}

/* Logo Section */
.logo-section {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 32px;
}

.logo-icon {
  width: 40px;
  height: 40px;
  background: #2C2C2C;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-symbol {
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 2px;
}

.logo-text {
  color: #1E1E1E;
  font-size: 24px;
  font-weight: 700;
  line-height: 32px;
}

/* Header Section */
.header-section {
  text-align: center;
  margin-bottom: 40px;
}

.welcome-title {
  color: #1E1E1E;
  font-size: 30px;
  font-weight: 700;
  line-height: 36px;
  margin: 0 0 12px 0;
}

.welcome-subtitle {
  color: #B3B3B3;
  font-size: 16px;
  font-weight: 400;
  line-height: 24px;
  margin: 0;
}

/* Form Styles */
.login-form {
  margin-bottom: 32px;
}

.form-group {
  margin-bottom: 24px;
}

.form-label {
  display: block;
  color: #1E1E1E;
  font-size: 14px;
  font-weight: 600;
  line-height: 21px;
  margin-bottom: 8px;
}

.input-wrapper {
  position: relative;
}

.form-input {
  width: 100%;
  height: 49px;
  background: white;
  border: 1px solid #D9D9D9;
  border-radius: 8px;
  padding: 0 16px;
  font-size: 16px;
  font-family: 'Inter', sans-serif;
  color: #1E1E1E;
  box-sizing: border-box;
}

.form-input::placeholder {
  color: #999999;
  font-size: 16px;
  font-weight: 400;
}

.form-input:focus {
  outline: none;
  border-color: #2C2C2C;
  box-shadow: 0 0 0 2px rgba(44, 44, 44, 0.1);
}

.password-toggle {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  width: 20px;
  height: 20px;
}

.eye-icon {
  width: 18.33px;
  height: 12.50px;
  background: black;
  border-radius: 2px;
  transition: opacity 0.3s;
}

.eye-icon.eye-closed {
  opacity: 0.5;
}

/* Resend Section */
.resend-section {
  text-align: center;
  margin-bottom: 24px;
}

.resend-text {
  color: #B3B3B3;
  font-size: 14px;
  font-weight: 400;
  line-height: 21px;
  margin-right: 8px;
}

.resend-button {
  background: none;
  border: none;
  color: #2C2C2C;
  font-size: 14px;
  font-weight: 600;
  line-height: 21px;
  cursor: pointer;
  text-decoration: underline;
  padding: 0;
}

.resend-button:disabled {
  color: #B3B3B3;
  cursor: not-allowed;
  text-decoration: none;
}

.resend-button:hover:not(:disabled) {
  color: #1a1a1a;
}

/* Form Options */
.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.checkbox {
  width: 16px;
  height: 16px;
  border: 1px solid #D9D9D9;
  border-radius: 4px;
  background: white;
  cursor: pointer;
}

.checkbox-label {
  color: #1E1E1E;
  font-size: 14px;
  font-weight: 400;
  line-height: 21px;
}

.forgot-password {
  color: #2C2C2C;
  font-size: 14px;
  font-weight: 400;
  line-height: 21px;
  text-decoration: none;
}

.forgot-password:hover {
  text-decoration: underline;
}

/* Login Button */
.login-button {
  width: 100%;
  height: 48px;
  background: #2C2C2C;
  border: none;
  border-radius: 8px;
  color: #F5F5F5;
  font-size: 16px;
  font-weight: 400;
  line-height: 24px;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 32px;
}

.login-button:hover:not(:disabled) {
  background: #1a1a1a;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(44, 44, 44, 0.3);
}

.login-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Divider */
.divider {
  position: relative;
  text-align: center;
  margin-bottom: 24px;
}

.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: #D9D9D9;
}

.divider-text {
  background: white;
  color: #B3B3B3;
  font-size: 14px;
  font-weight: 400;
  line-height: 21px;
  padding: 0 16px;
  position: relative;
  z-index: 1;
}

/* Social Login */
.social-login {
  display: flex;
  gap: 12px;
  margin-bottom: 32px;
}

.social-button {
  flex: 1;
  height: 46px;
  background: white;
  border: 1px solid #D9D9D9;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 14px;
  font-weight: 500;
  line-height: 21px;
  color: #1E1E1E;
}

.social-button:hover {
  background: #F8FAFC;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* Google Icon */
.google-icon {
  position: relative;
  width: 20px;
  height: 20px;
}

.google-blue {
  position: absolute;
  width: 8.80px;
  height: 8.62px;
  left: 10px;
  top: 8.33px;
  background: #4285F4;
}

.google-green {
  position: absolute;
  width: 14.25px;
  height: 7.42px;
  left: 1.82px;
  top: 11.75px;
  background: #34A853;
}

.google-yellow {
  position: absolute;
  width: 4.03px;
  height: 8.22px;
  left: 0.83px;
  top: 5.89px;
  background: #FBBC05;
}

.google-red {
  position: absolute;
  width: 14.32px;
  height: 7.42px;
  left: 1.82px;
  top: 0.83px;
  background: #EA4335;
}

/* Facebook Icon */
.facebook-icon {
  width: 20px;
  height: 19.88px;
  background: #1877F2;
  border-radius: 2px;
}

/* Sign Up Link */
.signup-link {
  text-align: center;
}

.signup-text {
  color: #B3B3B3;
  font-size: 14px;
  font-weight: 400;
  line-height: 21px;
  margin-right: 8px;
}

.signup-button {
  color: #2C2C2C;
  font-size: 14px;
  font-weight: 600;
  line-height: 21px;
  text-decoration: none;
}

.signup-button:hover {
  text-decoration: underline;
}

/* Responsive Design */
@media (max-width: 768px) {
  .login-page {
    padding: 16px;
  }

  .login-card {
    padding: 32px 24px;
  }

  .social-login {
    flex-direction: column;
  }
}

@media (max-width: 480px) {
  .welcome-title {
    font-size: 24px;
    line-height: 30px;
  }

  .welcome-subtitle {
    font-size: 14px;
  }

  .logo-text {
    font-size: 20px;
  }
}
.welcome-subtitle {
  font-size: 14px;
  line-height: 22px;
  color: #7D7D7D;
}
</style>
